import * as Api from "../../api/login";
const util = require("../../utils/util");
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    temp: {},
    mobile: "",
    countdown: 60, // 初始倒计时时间
    isCountingDown: false, // 是否正在倒计时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.login({
      success(res) {
        console.log(res);
        console.log(Api);
        app.globalData.code = res.code;
        // Api.getTokenCreate(_this, res.code).then((create) => {
        //   console.log(create);
        //   const { is_bind, token } = create;
        //   _this.globalData.token = token;
        //   _this.globalData.isBind = is_bind;
        //   wx.setStorageSync("token", token);
        //   wx.setStorageSync("isBind", is_bind);
        //   wx.navigateTo({
        //     url:
        //       "/pages/cabinetInfo/cabinetInfo?cabinet_number=" +
        //       _this.globalData.param,
        //   });
        // });
      },
    });
  },
  /**
   * @description: 发送验证码
   * @param {*} e
   * @return {*}
   */
  getMobile(e) {
    this.setData({ mobile: e.detail });
  },
  sendCode: function () {
    const { mobile } = this.data;
    if (this.data.isCountingDown) {
      return; // 如果正在倒计时，则不执行任何操作
    }
    if (!util.validatePhoneNumber(mobile)) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
      });
      return;
    }
    Api.getMobileCode(app, { action: "login", mobile }).then(() => {
      this.setData({
        isCountingDown: true, // 标记正在倒计时，防止重复点击
      });

      var interval = setInterval(() => {
        var countdown = this.data.countdown - 1;
        if (countdown > 0) {
          this.setData({
            countdown: countdown,
          });
        } else {
          clearInterval(interval); // 清除定时器
          this.setData({
            countdown: 60, // 重置倒计时时间
            isCountingDown: false, // 取消倒计时标记，启用发送验证码按钮
          });
        }
      }, 1000);
    });
  },

  formSubmit(e) {
    console.log(e);

    const { mobile, sms_code } = e.detail.value;
    if (!util.validatePhoneNumber(mobile)) {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: "none",
      });
      return;
    }
    if (!sms_code) {
      wx.showToast({
        title: "请输入验证码",
        icon: "none",
      });
      return;
    }

    Api.bindCustomMobile(app, {
      mobile,
      sms_code,
      code: app.globalData.code,
    }).then((res) => {
      app.globalData.token = res.token;
      wx.setStorageSync("token", res.token);
      wx.setStorageSync("is_login", true);
      app.globalData.is_login = true;
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2]; // 获取上一页

      // 返回上一页并传递参数关闭弹窗
      wx.navigateBack({
        delta: 1,
        success: function () {
          // 使用参数告诉上一页关闭弹窗
          prevPage.setData({
            visibleDialog: false,
          });
        },
      });
    });
  },

  /**
   * @description: 返回上一页
   * @return {*}
   */

  _navigateBack(status) {
    let currentPages = getCurrentPages();
    let page = currentPages[currentPages.length - 2];
    //要在返回之前删除信息
    if (status === 0) {
      wx.removeStorageSync("mobile");
      app.globalData.userInfo = {};
      app.globalData.token = "";
      wx.removeStorageSync("userInfo");
      wx.removeStorageSync("uid");
      wx.removeStorageSync("token");
    }
    wx.navigateBack({
      success: () => {
        if (page == undefined || page == null) return;
        if (status) {
          if (app.globalData.token && !page.loginSuccess) {
            page.onLoad(page.options);
          } else if (app.globalData.token && page.loginSuccess) {
            page.loginSuccess();
          }
        } else {
          page.onLoad(page.options);
        }
      },
    });
  },
});
